/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package foxmob.mlts.controller.setter;

import foxmob.mlts.component.FileManager;
import foxmob.mlts.model.QuestionPaper;
import foxmob.mlts.viewcontroller.setter.OpenTestRecordForm;
import java.util.ArrayList;

/**
 *
 * @author Lunayo
 */
public class OpenTestRecordFormController {
    
    private OpenTestRecordForm delegate;
    private SetterFormController setterFormDelegate;
    
    private QuestionPaper selectedRecord;
    
    public OpenTestRecordFormController(SetterFormController setterFormDelegate, 
            OpenTestRecordForm delegate) {
        this.setterFormDelegate = setterFormDelegate;
        this.delegate = delegate;
    }
    
    public boolean loadTestRecord() {
        FileManager manager = FileManager.getInstanceObject();
        ArrayList<QuestionPaper> records = manager.getTestRecord();
        if (records.size() <= 0) return false;
        delegate.getRecordList().setListData(records.toArray());
        return true;
    }
    
    public void openTestRecord() {
        // notify the delegate that the class has finished
        try {
            // error handling if the method was not found
            setterFormDelegate.openRecordDidFinish(selectedRecord);
            setterFormDelegate.getSetterFormDelegate().setVisible(true);
            delegate.setVisible(false);
        } catch (Exception e) {
            System.err.print("Error : " + e.getMessage());
        }
    }
     
    
    public void listSelectionDidSelect() {
        selectedRecord = (QuestionPaper)delegate.getRecordList().getSelectedValue();
    }
    
    public void okButtonDidTap() {
        openTestRecord();
    }
    
    public void recordListMouseDidClick(java.awt.event.MouseEvent evt) {
        if (evt.getClickCount() == 2) {
            openTestRecord();
        }
    }
}
